<template>
  <div>
    <div
      class="demo-upload-list"
      v-if="imgurl !== '' && imgurl !== null && imgurl !== undefined"
    >
      <template>
        <img :src="imgurl" />
        <div class="demo-upload-list-cover">
          <Icon
            type="ios-eye-outline"
            @click.native="handleView(imgurl)"
          ></Icon>
          <Icon
            type="ios-trash-outline"
            @click.native="handleRemove()"
          ></Icon>
        </div>
      </template>
    </div>
    <Upload
      v-if="!isOneImage || (isOneImage && defaultList.length == 0)"
      ref="upload"
      :show-upload-list="false"
      :headers="{ Authorization: store_token }"
      :default-file-list="defaultList"
      :on-success="handleSuccess"
      :format="['jpg', 'jpeg', 'png']"
      :max-size="2048"
      :on-format-error="handleFormatError"
      :on-exceeded-size="handleMaxSize"
      :before-upload="handleBeforeUpload"
      type="drag"
      :action="action"
      style="display: inline-block; width: 100%"
    >
      <div style="height: 125px; line-height: 125px">
        <Icon
          type="ios-camera"
          size="40"
        ></Icon>
        <!-- <span>点击上传</span> -->
      </div>
    </Upload>
    <Modal
      title="View Image"
      v-model="visible"
    >
      <img
        :src="imgName"
        v-if="visible"
        style="width: 100%"
      />
    </Modal>
  </div>
</template>
<script>
import Setting from "@/setting.env";
import { mapMutations, mapGetters } from "vuex";

export default {
  props: {
    isOneImage: {
      type: Boolean,
      default: true,
    }, // 是否仅上传一张图片 默认false
    action: {
      type: String,
      default: Setting.imageBaseURL
    },
    imageList: {
      type: Array,
      default: () => []
    },
    imageString: {
      type: String,
      default: ""
    }, //传入的图片地址
  },
  data() {
    return {
      defaultList: [],
      imgName: "",
      visible: false,
      uploadList: [],
      imgurl: ""
    };
  },
  watch: {
    imageString(val) {
      this.imgurl = val;
    },
    // 抛出图片地址
    imgurl(val) {
      this.$emit("changeval", val);
    }
  },
  computed: {
    ...mapGetters(["store_token"])
  },
  mounted() {
    this.imgurl = this.imageString;
  },
  methods: {
    // 图片上传成功
    handleSuccess(res, file) {
      this.imgurl = res.data.fileUrl;
      this.$emit('uploadsuccessimg', this.imgurl);
    },
    handleView(url) {
      this.imgName = url;
      this.visible = true;
    },
    handleRemove() {
      this.imgurl = "";
      // this.$emit('deleteimg',file)
      // this.imageList.splice(0, 1)
      // this.imageString = ''
    },
    handleFormatError(file) {
      this.$Notice.warning({
        title: "警告",
        desc: "文件 " + file.name + " 格式不正确"
      });
    },
    handleMaxSize(file) {
      this.$Notice.warning({
        title: "警告",
        desc: "文件  " + file.name + " 大小超过 2M."
      });
    },
    handleBeforeUpload() {
      //const check = this.uploadList.length < 5;
      // if (!check) {
      //     this.$Notice.warning({
      //         title: 'Up to five pictures can be uploaded.'
      //     });
      // }
      //return check;
    }
  },
};
</script>
<style>
.demo-upload-list {
  display: inline-block;
  width: 100%;
  height: 125px;
  text-align: center;
  line-height: 125px;
  border: 1px solid transparent;
  border-radius: 4px;
  overflow: hidden;
  background: #fff;
  position: relative;
  box-shadow: 0 1px 1px rgba(0, 0, 0, 0.2);
  margin-right: 4px;
}
.demo-upload-list img {
  width: 100%;
  height: 100%;
}
.demo-upload-list-cover {
  display: none;
  position: absolute;
  top: 0;
  bottom: 0;
  left: 0;
  right: 0;
  background: rgba(0, 0, 0, 0.6);
}
.demo-upload-list:hover .demo-upload-list-cover {
  display: block;
}
.demo-upload-list-cover i {
  color: #fff;
  font-size: 20px;
  cursor: pointer;
  margin: 0 2px;
}
</style>
